package cn.gz.sboot42.controller;

import cn.gz.sboot42.entity.Emp;
import cn.gz.sboot42.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController//@RestController=@Response+@Controller  @Response将方法的返回值以JSON字符串的形式响应给客户端
public class EmpController {

    @Autowired
    private EmpMapper empMapper;

    @RequestMapping("/add")
    public void add(Emp emp){
        empMapper.insert(emp);
    }

    @RequestMapping("/select")
    public List<Emp> select(){
        //SpringMVC框架会自动将返回的对象或者集合转化为JSON格式的字符串
        //然后将JSON格式的字符串转为二进制，网络传输给客户端
        List<Emp> emps = empMapper.select();
        return emps;
    }

    @RequestMapping("/delete")
    public void del(int id){
        empMapper.delete(id);
    }

    @RequestMapping("/selectOne")
    public Emp selectById(int id){
       return empMapper.selectById(id);
    }

    @RequestMapping("/update")   //如果前端直接把JS对象传递给后端，后端需要在接收参数前加注解@RequestBody
    public void update(@RequestBody Emp emp){
        System.out.println("emp = " + emp);
        empMapper.update(emp);
    }

}
